package middleware

import (
	"net/http"

	"github.com/tungyao/cedar"
)

func AuthLogin(w http.ResponseWriter, r *http.Request, co *cedar.Core) bool {
	if co.Session.Get("login") == nil {
		http.Redirect(w, r, "/manager/page/login", 302)
		return false
	}
	return true
}
func BookAuthLogin(w http.ResponseWriter, r *http.Request, co *cedar.Core) bool {
	username, password, ok := r.BasicAuth()
	if !ok {
		w.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`)
		w.WriteHeader(http.StatusUnauthorized)
		return false
	}
	if username != "tong" || password != "1121331" {
		http.Error(w, "RESET: need authorized!", http.StatusUnauthorized)
		return false
	}
	return true
}
